package com.pickgliss.loaders
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.ByteArray;

	public class DisplayLoader extends BaseLoader
	{
		protected var _displayLoader:Loader;
		public function DisplayLoader(url:String, callback:Function = null)
		{
			_displayLoader = new Loader();
			super(url, callback);
		}
		
		override public function loadFromBytes(data:ByteArray):void
		{
			super.loadFromBytes(data);
			_displayLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,__onContentLoadComplete);
			_displayLoader.loadBytes(data);
		}
		
		protected function __onContentLoadComplete(event:Event):void
		{
			_displayLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,__onContentLoadComplete);
			fireCompleteEvent();
		}
		
		override protected function __onDataLoadComplete(event:Event):void
		{
			removeEvent();
			_loader.close();
			if(_loader.data.length == 0)return;
			loadFromBytes(_loader.data);
		}
		
		override public function get content():*
		{
			return _displayLoader.content;
		}
		
		override protected function getLoadDataFormat():String
		{
			return URLLoaderDataFormat.BINARY;
		}
		
		override public function get type():int
		{
			return LoaderTypes.DISPLAY_LOADER;
		}
		
		public function get display():DisplayObject
		{
			return _displayLoader.content as DisplayObject;
		}
		
		override public function dispose():void
		{
			_displayLoader.unload();
			_displayLoader = null;
		}
	}
}